{include file="public/header,public/nav,public/fixbar" title="我的收藏-<?php echo request()->pc_info['seo_title']?>"/}
<div class="layui-body">
    <div class="layui-container">
        <div class="layui-col-space15">
            <!-- 菜单栏 -->
            <div class="layui-col-xs12 layui-col-sm4 layui-col-md4 layui-col-lg3">
                <div class="layui-main user-main min-height-90vh">
                    {include file="user/menu"/}
                </div>
            </div>
            <div class="layui-col-xs12 layui-col-sm8 layui-col-md8 layui-col-lg9">
                <div class="layui-main user-main min-height-90vh">
                    <h2>我的收藏</h2>
                    <div class="layui-tab layui-tab-brief padding-bottom-10">
                        <ul class="layui-tab-title">
                            {foreach $getPcProductType as $key => $v}
                            <li class="{if $key == 1}layui-this{/if} select-type" data-type="{$key}">{$v}</li>
                            {/foreach}
                        </ul>
                    </div>
                    <div class="layui-row">
                        <div class="layui-col-space15 scrollbar height-700" id="favorites-list">
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer"/}
<script type="text/javascript">
    layui.use(['flow','layer'], function(){
        var layer = layui.layer, flow = layui.flow, $=layui.jquery;
        
        var product_type = 1;
        $(function(){
            loadList(product_type);  // 加载列表
        })

        // 根据类型读取
        $(".select-type").on('click', function(){
            product_type = $(this).data('type');
            $('#favorites-list').html('');
            loadList(product_type);
        });

        // 分页加载列表
        function loadList(product_type){
            var limit = 10;//每次请求的条数
            flow.load({
                elem: '#favorites-list' //指定列表容器
                ,scrollElem: '#favorites-list'//滚动条所在元素
                ,end: '—— 已经到底了~ ——'
                ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页的回调
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        data: {'page': page, 'limit':limit, 'product_type':product_type},//请求的页码和每页显示条数
                        url: '/user/favorites',
                        success: function (res) {
                            var lis = [];
                            if(res.code == 0){
                                if (res.data.length > 0) {
                                    
                                    var newsHtml = '';
                                    layui.each(res.data, function(index, item){
                                        var img = `<img src="${item.head_imgs}" class="user-course-img">`;
                                        if (item.product_type == 1) {
                                            img = `<img src="${item.head_imgs}" class="user-goods-img">`;
                                        } else if (item.product_type == 10){
                                            img = `<div class="justify-content-center">
                                                        <img style="background-color: ${item.background_color};margin-right: 0px;" src="${item.head_imgs}" class="user-goods-img">
                                                        <span class="exam-sub-title">${item.sub_title}</span>
                                                    </div>`;
                                        }
                                        var price = '<p class="product-price">免费</p>';
                                        if (item.sale_price > 0) {
                                            price = `<p class="product-price"><span class="symbol">￥</span>${item.sale_price}</p>`;
                                        }
                                        newsHtml += `<div class="layui-col-xs12 layui-col-sm6 layui-col-md6">
                                                        <div class="layui-card product-item">
                                                            <a href="${item.href}">
                                                                <div class="layui-card-body">
                                                                    <div class="item">
                                                                        ${img}
                                                                        <div class="detail height-80">
                                                                            <div class="product-title layui-ellips">
                                                                                <span class="item-title-label">${item.product_type_text}</span>${item.name}
                                                                            </div>
                                                                            ${price}
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </a>
                                                            <div class="layui-card-footer">
                                                                <p class="text">已售：${item.virtual_sales + item.real_sales}</span></p>
                                                                <span class="layui-btn layui-btn-primary layui-btn-sm favorites-del" data-id="${item.product_id}"><i class="layui-icon layui-icon-delete"></i> 移除</span>
                                                            </div>
                                                        </div>
                                                    </div>`;
                                    });
                                    
                                    lis.push(newsHtml);
                                    //执行下一页渲染，第二参数为：满足“加载更多”的条件，即后面仍有分页
                                    //res.total为Ajax返回的总页数，只有当前页小于总页数的情况下，才会继续出现加载更多
                                    next(lis.join(''), page < Math.ceil(res.count/limit));
                                    
                                } else {
                                    $('#favorites-list').html('<div class="layui-empty"><img src="/static/images/nodata/no_favorites.png"><p>暂无收藏商品~</p></div>');
                                }
                            } else {
                                layer.msg(res.msg, {icon:2, time: 1500});
                            }
                        }
                        ,error:function(e){
                            layer.closeAll();
                            layer.msg("请求失败："+e.msg, {icon:2, time: 1500});
                        },
                    });
                }
            });
        }

        // 取消收藏
        $(document).on('click', '.favorites-del',function(){
            var product_id = $(this).data('id');
            layer.confirm('确定取消收藏吗？', {
                skin: 'layui-layer-radius', // 弹窗主题样式
                title : '操作确认',
            },function(index){
                var loading = layer.msg('数据处理中..', {icon: 16,shade: 0.3,time: false});
                $.ajax({
                    url:"/user/favorites_del",
                    type:'post',
                    data:{"product_id":product_id},
                    dataType:'JSON',
                    headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                    success:function(res){
                        layer.close(loading);
                        if(res.code==0){
                            layer.msg(res.msg,{icon: 1,time: 1500},function(){
                                $('#favorites-list').html('');
                                loadList(product_type);
                            });
                        }else{
                            layer.msg(res.msg,{icon: 2,time: 2000});
                        }
                    },
                    error:function(e){
                        layer.msg('请求失败'+e.responseText,{icon: 2});
                    },
                });
            })
        })
    });

</script>